package com.bwie.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.alibaba.fastjson.JSONObject;
import com.bwie.pojo.Result;
import com.bwie.pojo.dto.ChargingDto;
import com.bwie.pojo.excel.ChargingExcel;
import com.bwie.pojo.po.Charging;
import com.bwie.service.ChargingService;
import com.bwie.utils.poi.EasyPoiUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/charging")
public class ChargingController {
    @Autowired
    private ChargingService chargingService;
    @Autowired
    private EasyPoiUtil easyPoiUtil;

    @ResponseBody
    @GetMapping("getChargingList")
    public PageInfo<Charging> getChargingList(ChargingDto chargingDto){
        PageInfo<Charging> pageInfo = chargingService.getCharging(chargingDto);
        return pageInfo;
    }

    @ResponseBody
    @PostMapping("addCharging")
    public Result addChargingList(@RequestBody Charging charging){
        chargingService.addChargingList(charging);
        return Result.success();
    }




    @ResponseBody
    @PostMapping("uploadList")
    public Result uploadList(@RequestParam("file") MultipartFile file) throws Exception {
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        List<Charging> list = ExcelImportUtil.importExcel(file.getInputStream(), Charging.class, importParams);

        return Result.success();
    }



    /**
     * 选中导出:
     */
    @RequestMapping("exportExcelFileByCk")
    public void exportExcelFileByCk(HttpServletResponse response, @RequestParam("ids") List<Integer> ids2) {
        //1.根据ids查询对应的数据-集合
        List<ChargingExcel> list = chargingService.selectListByIds(ids2);
        //2.调用导出工具类
        easyPoiUtil.exportExcel(response, list, ChargingExcel.class, "选择导出收费规则报表");
    }

    /**
     * 导出所有数据
     */
    @RequestMapping("exportExcelFile")
    public void exportExcelFile(HttpServletResponse response) {
        //1.去数据库查询要导出的数据-集合
        List<ChargingExcel> list = chargingService.selectListAll();
        //2.调用导出工具类
        easyPoiUtil.exportExcel(response, list, ChargingExcel.class, "收费规则报表导出");
    }


    /**
     * 实现导入功能
     *
     * @param file
     * @return
     */
    @ResponseBody
    @PostMapping("importExcel")
    public Result importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        //1.使用easyPoi工具栏解析导入Excel文件内容-list
        ImportParams importParams = new ImportParams();//导入参数
        importParams.setHeadRows(1);//从下标为2开始解析
        List<Charging> list = ExcelImportUtil.importExcel(file.getInputStream(), Charging.class, importParams);
        //2.批量添加
        chargingService.batchAdd(list);

        return Result.success("导入成功");

    }




    @RequestMapping("toChargingDome")
    public String toChargingDemo(Model model){
        List<BigDecimal> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        List<Charging> chargingList = chargingService.getChargingList();
        for (Charging charging : chargingList) {
            list.add(charging.getChargingPrice());
            list1.add(charging.getChargingName());
        }


        model.addAttribute("list", JSONObject.toJSONString(list));
        model.addAttribute("list1", JSONObject.toJSONString(list1));


        ArrayList<BigDecimal> list2 = new ArrayList<>();
        ArrayList<BigDecimal> list3 = new ArrayList<>();
        for (Charging charging : chargingList) {
            if (charging.getChargingType()==0){
                list2.add(charging.getChargingPrice());
            }
            if (charging.getChargingType()==1){
                list3.add(charging.getChargingPrice());
            }
        }

        model.addAttribute("list2", JSONObject.toJSONString(list2));
        model.addAttribute("list3", JSONObject.toJSONString(list3));



        return "/charging/demo";
    }











}
